package com.streamer.app.utils

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.streamer.app.R
import android.widget.ImageView

object DialogUtils {

    /**
     * 弹窗配置类
     */
    data class DialogConfig(
        val title: String = "温馨提示",
        val message: String,
        val cancelText: String = "取消",
        val confirmText: String = "确定",
        val showCancelButton: Boolean = true,
        val showConfirmButton: Boolean = true,
        val cancelable: Boolean = true,
        val widthPercent: Float = 0.8f,
        val onConfirm: (() -> Unit)? = null,
        val onCancel: (() -> Unit)? = null
    )

    /**
     * 显示确认弹窗（使用配置类）
     */
    fun showConfirmDialog(context: Context, config: DialogConfig) {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_confirm, null)
        
        // 设置标题
        dialogView.findViewById<TextView>(R.id.tv_dialog_title).text = config.title
        
        // 设置消息内容
        dialogView.findViewById<TextView>(R.id.tv_dialog_message).text = config.message
        
        // 设置按钮
        val cancelButton = dialogView.findViewById<TextView>(R.id.btn_dialog_cancel)
        val confirmButton = dialogView.findViewById<TextView>(R.id.btn_dialog_confirm)
        
        cancelButton.text = config.cancelText
        confirmButton.text = config.confirmText
        
        // 控制按钮显示
        cancelButton.visibility = if (config.showCancelButton) View.VISIBLE else View.GONE
        confirmButton.visibility = if (config.showConfirmButton) View.VISIBLE else View.GONE
        
        val dialog = AlertDialog.Builder(context)
            .setView(dialogView)
            .setCancelable(config.cancelable)
            .create()
        
        // 设置弹窗样式
        dialog.window?.let { window ->
            // 设置背景透明
            window.setBackgroundDrawableResource(android.R.color.transparent)
            
            // 设置弹窗宽度
            val displayMetrics = context.resources.displayMetrics
            val width = (displayMetrics.widthPixels * config.widthPercent).toInt()
            window.setLayout(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
            
            // 设置弹窗动画
            window.attributes.windowAnimations = R.style.DialogAnimation
        }
        
        // 设置按钮点击事件
        cancelButton.setOnClickListener {
            config.onCancel?.invoke()
            dialog.dismiss()
        }
        
        confirmButton.setOnClickListener {
            config.onConfirm?.invoke()
            dialog.dismiss()
        }
        
        // 关闭按钮
        dialogView.findViewById<View>(R.id.iv_dialog_close).setOnClickListener {
            config.onCancel?.invoke()
            dialog.dismiss()
        }
        
        // 设置点击外部关闭
        dialog.setOnCancelListener {
            config.onCancel?.invoke()
        }
        
        dialog.show()
    }

    /**
     * 显示确认弹窗（简化版本）
     * @param context 上下文
     * @param title 标题
     * @param message 消息内容
     * @param cancelText 取消按钮文字
     * @param confirmText 确认按钮文字
     * @param onConfirm 确认回调
     * @param onCancel 取消回调
     */
    fun showConfirmDialog(
        context: Context,
        title: String = "温馨提示",
        message: String,
        cancelText: String = "取消",
        confirmText: String = "确定",
        onConfirm: (() -> Unit)? = null,
        onCancel: (() -> Unit)? = null
    ) {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_confirm, null)
        
        // 设置标题
        dialogView.findViewById<TextView>(R.id.tv_dialog_title).text = title
        
        // 设置消息内容
        dialogView.findViewById<TextView>(R.id.tv_dialog_message).text = message
        
        // 设置按钮文字
        dialogView.findViewById<TextView>(R.id.btn_dialog_cancel).text = cancelText
        dialogView.findViewById<TextView>(R.id.btn_dialog_confirm).text = confirmText
        
        val dialog = AlertDialog.Builder(context)
            .setView(dialogView)
            .setCancelable(false)
            .create()
        
        // 设置弹窗样式
        dialog.window?.let { window ->
            // 设置背景透明
            window.setBackgroundDrawableResource(android.R.color.transparent)
            
            // 设置弹窗宽度为屏幕宽度的80%
            val displayMetrics = context.resources.displayMetrics
            val width = (displayMetrics.widthPixels * 0.8).toInt()
            window.setLayout(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
            
            // 设置弹窗动画
            window.attributes.windowAnimations = R.style.DialogAnimation
        }
        
        // 设置按钮点击事件
        dialogView.findViewById<TextView>(R.id.btn_dialog_cancel).setOnClickListener {
            onCancel?.invoke()
            dialog.dismiss()
        }
        
        dialogView.findViewById<TextView>(R.id.btn_dialog_confirm).setOnClickListener {
            onConfirm?.invoke()
            dialog.dismiss()
        }
        
        // 关闭按钮
        dialogView.findViewById<View>(R.id.iv_dialog_close).setOnClickListener {
            onCancel?.invoke()
            dialog.dismiss()
        }
        
        // 设置点击外部关闭
        dialog.setOnCancelListener {
            onCancel?.invoke()
        }
        
        dialog.show()
    }

    /**
     * 显示确认关闭直播
     */
    fun showEndLiveMuteConfirmDialog(
        context: Context,
        onConfirm: (() -> Unit)? = null
    ) {
        val message = "您确定要关闭直播间并离开直播间"
        showConfirmDialog(
            context = context,
            message = message,
            onConfirm = onConfirm
        )
    }
    fun showConfirmLoginConfirmDialog(
        context: Context,
        onConfirm: (() -> Unit)? = null
    ) {
        val message = "已有老师，继续进入将导致该老师强制下线"
        showConfirmDialog(
            context = context,
            message = message,
            onConfirm = onConfirm
        )
    }
    /**
     * 显示禁言确认弹窗
     */
    fun showMuteConfirmDialog(
        context: Context,
        userName: String,
        onConfirm: (() -> Unit)? = null
    ) {
        val message = "您确定将\"$userName\"禁言?\n禁言后该用户发送的消息其他人将无法查看"
        showConfirmDialog(
            context = context,
            message = message,
            onConfirm = onConfirm
        )
    }

    /**
     * 显示踢出房间确认弹窗
     */
    fun showKickConfirmDialog(
        context: Context,
        userName: String,
        onConfirm: (() -> Unit)? = null
    ) {
        val message = "您确定将\"$userName\"踢出房间?\n踢出房间后该用户将被加入该直播间的黑名单且不能再进入此房间"
        showConfirmDialog(
            context = context,
            message = message,
            onConfirm = onConfirm
        )
    }

    /**
     * 显示加入黑名单确认弹窗
     */
    fun showBlacklistConfirmDialog(
        context: Context,
        userName: String,
        onConfirm: (() -> Unit)? = null
    ) {
        val message = "您确定将\"$userName\"加入黑名单?\n加入黑名单后该用户将无法进入任何直播间"
        showConfirmDialog(
            context = context,
            message = message,
            onConfirm = onConfirm
        )
    }

    /**
     * 显示解除禁言确认弹窗
     */
    fun showUnmuteConfirmDialog(
        context: Context,
        userName: String,
        onConfirm: (() -> Unit)? = null
    ) {
        val message = "您确定解除\"$userName\"的禁言?\n解除禁言后该用户可以正常发送消息"
        showConfirmDialog(
            context = context,
            message = message,
            onConfirm = onConfirm
        )
    }

    fun showAnnouncementDialog(context: Context, title: String, content: String) {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_announcement, null)
        val tvTitle = dialogView.findViewById<TextView>(R.id.tv_announcement_title)
        val tvContent = dialogView.findViewById<TextView>(R.id.tv_announcement_content)
        val ivClose = dialogView.findViewById<ImageView>(R.id.iv_announcement_close)
        val btnConfirm = dialogView.findViewById<TextView>(R.id.btn_dialog_confirm)
        tvTitle.text = title
        tvContent.text = content
        val dialog = AlertDialog.Builder(context)
            .setView(dialogView)
            .setCancelable(true)
            .create()
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        ivClose.setOnClickListener { dialog.dismiss() }
        btnConfirm.setOnClickListener { dialog.dismiss() }
        dialog.show()
    }
} 